package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/*
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理接口")
@Slf4j
public class DishController {

    @Resource(name = "dishServiceImpl")
    DishService dishService;

    @ApiOperation("新增菜品")
    @PostMapping
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result addDish(@RequestBody DishDTO dishDTO) {
        boolean res = dishService.addDish(dishDTO);
        return res?Result.success():Result.error("异常事件请联系管理员");

    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> dishPageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品...");
        PageResult pageResult = dishService.selDishAll(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过id查询菜品")
    public Result<DishVO> selDishById(@PathVariable Long id){
        log.info("通过id查询菜品");
        DishVO dishVO = dishService.selDishById(id);
        return Result.success(dishVO);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> selDishByCategoryId(Long categoryId){
        List<Dish> dishList = dishService.selDishByCategoryId(categoryId);
        return Result.success(dishList);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品启用禁用分类")
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){
        dishService.startOrStop(status,id);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result delDishById(@RequestParam List<Long> ids){
        dishService.delDishById(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result updateTime(@RequestBody DishDTO dishDTO){
        dishService.updateTime(dishDTO);
        return Result.success();
    }
}
